import 'dart:math';
import 'package:flutter/material.dart';

class ColorUtil {
  static Color getColor(Color color) {
    Color tmpColor = gammaCorrect(color, 0.5);
    //tmpColor = gammaCorrect(tmpColor, 0.5);
    return tmpColor;
  }

  // 定义伽马校正函数
  static Color gammaCorrect(Color color, double gamma) {
    // 提取颜色通道
    int r = (color.value & 0xFF0000) >> 16;
    int g = (color.value & 0x00FF00) >> 8;
    int b = (color.value & 0x0000FF);

    // 将颜色通道值转换为浮点数，并进行伽马校正
    num rFloat = r / 255.0;
    num gFloat = g / 255.0;
    num bFloat = b / 255.0;

    rFloat = pow(rFloat, 1 / gamma);
    gFloat = pow(gFloat, 1 / gamma);
    bFloat = pow(bFloat, 1 / gamma);

    // 将浮点数转换回整数
    int rNew = (rFloat * 255).round();
    int gNew = (gFloat * 255).round();
    int bNew = (bFloat * 255).round();

    // 组合新的颜色值
    return Color((0xFF000000 | (rNew << 16) | (gNew << 8) | bNew));
  }
}
